using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net.Mail;
using System.Net.Configuration;
using System.Configuration;
using System.Text.RegularExpressions;
using Ent.Log;

namespace Ent.Web.Mail
{
    public class Processor
    {
        public enum MailAddressType
        {
            From = 1,
            Bcc
        }
        public string To = "";
        //public string From = "";
        public string Subject = "";
        public string CC = "";
        public string BCC = "";
        public string Body = "";
        public static Logger Log
        {
            get
            {
                Logtype type = (Logtype)Enum.Parse(typeof(Logtype), ConfigurationManager.AppSettings.Get("LogLevel"));
                string logPath = ConfigurationManager.AppSettings.Get("LogPath");
                return Logger.GetLogger(HttpRuntime.AppDomainAppPath + "/" + logPath, "default", type);
            }
        }
        private static MailAddress _from = null;

        public static void SendEmail(string to, string subject, string body)
        {
            SendEmail(to, subject, body, From, string.Empty);
        }

        public static void SendEmail(string to, string subject, string body, string from)
        {
            SendEmail(to, subject, body, from, MailAddressType.From);
        }

        public static void SendEmail(string to, string subject, string body, string addresses, MailAddressType addressType)
        {
            MailAddress from = From;
            string bcc = string.Empty;

            if (MailAddressType.From == addressType)
            {
                from = new MailAddress(addresses);
            }
            else
            {
                bcc = addresses;
            }

            SendEmail(to, subject, body, from, bcc);
        }

        private static void SendEmail(string to, string subject, string body, MailAddress from, string bcc)
        {
            bool allowNotification = false;
            Boolean.TryParse(ConfigurationManager.AppSettings["allowNotification"], out allowNotification);
            if (!allowNotification)
                return;
            MailMessage message = new MailMessage();
            message.From = From;
            message.To.Add(to);
            if (!string.IsNullOrEmpty(bcc))
            {
                message.Bcc.Add(bcc);
            }
            message.ReplyTo = from;
            message.Subject = subject;
            message.Body = body; // HttpContext.Current.Server.HtmlEncode(body);
            message.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Send(message);
            Common.Log.Info("Mail sent.");
        }

        public static MailAddress From
        {
            get
            {
                if (null == _from)
                {
                    SmtpSection section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                    string address = section.From;
                    string displayName = ConfigurationManager.AppSettings["fromEmailDisplayName"];
                    _from = new MailAddress(address, displayName);
                }
                return _from;
            }
        }

        public void SendMail()
        {
            Log.Info("[To:" + To + ",From:" + From + ",Message:" + Body + "]");
            try
            {
                SendEmail(To, Subject, Body);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
        }
        public static bool IsValidEmail(string strIn)
        {
            // Return true if strIn is in valid e-mail format.
            return Regex.IsMatch(strIn,
                   @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
        }

    }
}
